﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Language;
using Portal.Interface;
using Portal.Framework;


namespace Portal.CDSP
{
    public class CDSPPaging
    {
        public string GetPaging(string TabModuleId, string belongSite, int MaxItems, int ItemsPerRow, int NoOfVisiblePage, int CurrentPage, string JumpJavaScript, ref int StartIndex, ref int EndIndex)
        {
            int TotalPage = MaxItems / ItemsPerRow;
            if (MaxItems % ItemsPerRow != 0) TotalPage++;

            if (TotalPage <= 1)
            {
                StartIndex = 0;
                EndIndex = MaxItems;
                return "";
            }

            StartIndex = ItemsPerRow * CurrentPage;
            EndIndex = StartIndex + ItemsPerRow;
            if (EndIndex > MaxItems) EndIndex = MaxItems;

            int TotalSegment = TotalPage / NoOfVisiblePage;
            if (TotalPage % NoOfVisiblePage != 0) TotalSegment++;
            //doan nay tinh xem no nam o segment may
            int CurrentSegment = 0;
            for (int iSegment = 0; iSegment < TotalSegment; iSegment++)
            {
                int FromPage = iSegment * NoOfVisiblePage;
                int EndPage = FromPage + NoOfVisiblePage;
                if (EndPage > TotalPage) EndPage = TotalPage;
                if (CurrentPage >= FromPage && CurrentPage < EndPage)
                {
                    CurrentSegment = iSegment;
                    break;
                }
            }

            int StartPageIndex = CurrentSegment * NoOfVisiblePage;
            int EndPageIndex = StartPageIndex + NoOfVisiblePage;
            if (EndPageIndex > TotalPage) EndPageIndex = TotalPage;
            string Html =
                // "<div></div>" + Portal.Language.ResourceBLL.GetText("Paging", "PagingText", "Trang ") + (CurrentPage + 1) + "/" + (TotalPage) + "&nbsp;";

                   // "<ul class=\"page_no\">" + Language.ResourceBLL.GetText("Paging", "PagingText", "Phân trang: ");
                // "<li class=cssPagingTitle>" + Language.ResourceBLL.GetText("Paging", "PagingText", "Phân trang: ") + "</li>";
            "<div class=\"pagenavi\"><ul><li class=first><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + StartPageIndex + ",'" + JumpJavaScript + "');\">" + new WebPart().GetText("Cmspaging_Fisrt", "đầu") + "</a></li>";
            if (CurrentPage > 0)
            {
                //"Trang 1/5<a href=\"\" class=\"selected\">[1]</a><a href=\"\">[2]</a><a href=\"\">[3]</a><a href=\"\">[Next]</a>"+
                //Html += "<a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + ",0,'" + JumpJavaScript + "');\"><<</a>&nbsp;";
                Html += "<li class=prev><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (CurrentPage - 1) + ",'" + JumpJavaScript + "');\">" + new WebPart().GetText("Cmspaging_Previous", "trước") + "</a></li>&nbsp;";
            }

            for (int iIndex = StartPageIndex; iIndex < EndPageIndex; iIndex++)
            {
                if (iIndex != CurrentPage)
                {
                    Html += "<li><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + iIndex + ",'" + JumpJavaScript + "');\">" + (iIndex + 1) + "</a></li>&nbsp;";
                }
                else
                {
                    Html += "<li class=\"current\"><a  href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + iIndex + ",'" + JumpJavaScript + "');\">" + (iIndex + 1) + "</a></li>&nbsp;";
                }
            }
            //Html += "<li class=next><a href=#>tiếp</a></li>" +

            if (CurrentPage < TotalPage - 1)
            {
                Html += "<li class=next><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (CurrentPage + 1) + ",'" + JumpJavaScript + "');\">" + new WebPart().GetText("Cmspaging_Next", "tiếp") + "</a></li>&nbsp;";
                //Html += "<a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (TotalPage - 1) + ",'" + JumpJavaScript + "');\">>></a>&nbsp;";
            }
            Html += 
                " <li class=end><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (EndPageIndex - 1) + ",'" + JumpJavaScript + "');\">" + new WebPart().GetText("Cmspaging_Last", "cuối") + "</a></li></ul> <br class=colfix />"+
                "</div>";
            return Html;
        }
    }
}
